﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MvcModel.Models;

namespace MvcModel.Controllers
{
    public class MusicStoreController : Controller
    {
        private MusicStoreDB db = new MusicStoreDB();

        // GET: MusicStore
        public ActionResult Index()
        {
            //lamaba表达式是一种匿名方法(也有可能是数据结构) 即没有方法名的方法

            //语法：形参列表=>方法体 delegate int del(int a) del myDelegate=x=>x*x;int j=myDelegate(5)//j=25;
            //这里表达lamdba的是一个匿名函数，也是实例化委托的参数
            // Func<int, int, int> func = (x, y) => x * y;
            //这里的lamada表达式是一个数据结构
            // Expression <Func<int,int,int>> expression = (X, Y) => X * Y;
            //表达式目录树转委托
            //var aa = expression.Compile().Invoke(10, 20);
            //Console.WriteLine(aa);


            // //Func 和 Action类是特殊类型 他们允许你在不必指定自定义委托类型的情况下去使用委托
            //实例化
            //Func<int,int,int> func =new Func<int, int, int>(Add);
            //匿名方法
            //Func<int, int, int> func = delegate (int a, int b) { return a + b; };
            //Lambda表达式
            //Func<int, int, int> func = (a, b) => { return a + b; };
            //var aaa = func(10, 20);
            //Console.WriteLine(aaa);

            //Action<int,string> eat = Eat;
            //eat(5, "苹果"); 
            //匿名
            //Action<int, string> eat = delegate (int num, string name) { Console.WriteLine($"我吃了{num}个{name}"); };
            //lamada表达式
            Action<int, string> eat = (int num, string name) =>{ Console.WriteLine($"我吃了{num}个{name}"); };
            eat(5,"苹果");
            var albums = db.Albums.Include(a => a.Artist).Include(a => a.Genre);
            return View(albums.ToList());
        }
        //private int Add(int A, int B)
        //{
        //    return A + B;
        //}
        private static void Eat(int num, string name)
        {
            Console.WriteLine($"我吃了{num}个{name}");

        }
        // GET: MusicStore/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Album album = db.Albums.Find(id);
            if (album == null)
            {
                return HttpNotFound();
            }
            return View(album);
        }

        // GET: MusicStore/Create
        public ActionResult Create()
        {
            ViewBag.ArtistId = new SelectList(db.Artists, "Id", "Name");
            ViewBag.GenreId = new SelectList(db.Genres, "Id", "Name");
            return View();
        }

        // POST: MusicStore/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "id,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album)
        {
            if (ModelState.IsValid)
            {
                db.Albums.Add(album);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ArtistId = new SelectList(db.Artists, "Id", "Name", album.ArtistId);
            ViewBag.GenreId = new SelectList(db.Genres, "Id", "Name", album.GenreId);
            return View(album);
        }

        // GET: MusicStore/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Album album = db.Albums.Find(id);
            if (album == null)
            {
                return HttpNotFound();
            }
            ViewBag.ArtistId = new SelectList(db.Artists, "Id", "Name", album.ArtistId);
            ViewBag.GenreId = new SelectList(db.Genres, "Id", "Name", album.GenreId);
            return View(album);
        }

        // POST: MusicStore/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "id,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album)
        {
            if (ModelState.IsValid)
            {
                db.Entry(album).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ArtistId = new SelectList(db.Artists, "Id", "Name", album.ArtistId);
            ViewBag.GenreId = new SelectList(db.Genres, "Id", "Name", album.GenreId);
            return View(album);
        }

        // GET: MusicStore/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Album album = db.Albums.Find(id);
            if (album == null)
            {
                return HttpNotFound();
            }
            return View(album);
        }

        // POST: MusicStore/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Album album = db.Albums.Find(id);
            db.Albums.Remove(album);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
